import numpy as np
from sympy import *
import matplotlib.pyplot as plt
import pandas as pd
#import seaborn as sns
#sns.set_style("whitegrid")
import math
from scipy import signal as sig
user1_1 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp01_user01.txt"), columns=['X','Y','Z'])
user1_2 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp02_user01.txt"), columns=['X','Y','Z'])
user2_1 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp03_user02.txt"), columns=['X','Y','Z'])
user2_2 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp04_user02.txt"), columns=['X','Y','Z'])
user3_1 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp05_user03.txt"), columns=['X','Y','Z'])
user3_2 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp06_user03.txt"), columns=['X','Y','Z'])
user4_1 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp07_user04.txt"), columns=['X','Y','Z'])
user4_2 = pd.DataFrame(data=np.loadtxt("Datasets/acc_exp08_user04.txt"), columns=['X','Y','Z'])
freq = 50 # Hz
T = 1/freq # sec
user1_1['Time (min)'] = np.arange(0, len(user1_1['X']) * T, T)/60
user1_2['Time (min)'] = np.arange(0, len(user1_2['X']) * T, T)/60
user2_1['Time (min)'] = np.arange(0, len(user2_1['X']) * T, T)/60
user2_2['Time (min)'] = np.arange(0, len(user2_2['X']) * T, T)/60
user3_1['Time (min)'] = np.arange(0, len(user3_1['X']) * T, T)/60
user3_2['Time (min)'] = np.arange(0, len(user3_2['X']) * T, T)/60
user4_1['Time (min)'] = np.arange(0, len(user4_1['X']) * T, T)/60
user4_2['Time (min)'] = np.arange(0, len(user4_2['X']) * T, T)/60
users_exp = [user1_1, user1_2, user2_1, user2_2, user3_1, user3_2, user4_1, user4_2]
pd.to_pickle(users_exp, "users.dat")
for i in range(len(users_exp)):
print("//" + "_"*9 + f"Sujeito {i//2+1} Experiencia {i%2+1}" + "_"*9 + "\\\\", end = '\n\n')
users_exp[i].info()
print("\n")
//_________Sujeito 1 Experiencia 1_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 20598 entries, 0 to 20597 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 20598 non-null float64 1 Y 20598 non-null float64 2 Z 20598 non-null float64 3 Time (min) 20598 non-null float64 dtypes: float64(4) memory usage: 643.8 KB //_________Sujeito 1 Experiencia 2_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 19286 entries, 0 to 19285 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 19286 non-null float64 1 Y 19286 non-null float64 2 Z 19286 non-null float64 3 Time (min) 19286 non-null float64 dtypes: float64(4) memory usage: 602.8 KB //_________Sujeito 2 Experiencia 1_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 18026 entries, 0 to 18025 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 18026 non-null float64 1 Y 18026 non-null float64 2 Z 18026 non-null float64 3 Time (min) 18026 non-null float64 dtypes: float64(4) memory usage: 563.4 KB //_________Sujeito 2 Experiencia 2_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 16565 entries, 0 to 16564 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 16565 non-null float64 1 Y 16565 non-null float64 2 Z 16565 non-null float64 3 Time (min) 16565 non-null float64 dtypes: float64(4) memory usage: 517.8 KB //_________Sujeito 3 Experiencia 1_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 20994 entries, 0 to 20993 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 20994 non-null float64 1 Y 20994 non-null float64 2 Z 20994 non-null float64 3 Time (min) 20994 non-null float64 dtypes: float64(4) memory usage: 656.2 KB //_________Sujeito 3 Experiencia 2_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 17493 entries, 0 to 17492 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 17493 non-null float64 1 Y 17493 non-null float64 2 Z 17493 non-null float64 3 Time (min) 17493 non-null float64 dtypes: float64(4) memory usage: 546.8 KB //_________Sujeito 4 Experiencia 1_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 17668 entries, 0 to 17667 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 17668 non-null float64 1 Y 17668 non-null float64 2 Z 17668 non-null float64 3 Time (min) 17668 non-null float64 dtypes: float64(4) memory usage: 552.2 KB //_________Sujeito 4 Experiencia 2_________\\ <class 'pandas.core.frame.DataFrame'> RangeIndex: 15888 entries, 0 to 15887 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 X 15888 non-null float64 1 Y 15888 non-null float64 2 Z 15888 non-null float64 3 Time (min) 15888 non-null float64 dtypes: float64(4) memory usage: 496.6 KB
for i in range(len(users_exp)):
print("//" + "_"*15 + f"Sujeito {i//2+1} Experiencia {i%2+1}" + "_"*15 + "\\\\", end='\n\n')
print(users_exp[i].describe())
print("\n")
//_______________Sujeito 1 Experiencia 1_______________\\
X Y Z Time (min)
count 20598.000000 20598.000000 20598.000000 20598.000000
mean 0.880673 -0.101714 0.097088 3.432833
std 0.346449 0.340091 0.285555 1.982092
min -0.647222 -1.209722 -0.676389 0.000000
25% 0.805556 -0.285764 -0.081944 1.716417
50% 0.981945 -0.202778 -0.004167 3.432833
75% 1.019445 -0.031944 0.204167 5.149250
max 1.950000 1.036111 1.269444 6.865667
//_______________Sujeito 1 Experiencia 2_______________\\
X Y Z Time (min)
count 19286.000000 19286.000000 19286.000000 19286.000000
mean 0.886662 -0.119086 -0.014771 3.214167
std 0.305887 0.410941 0.240264 1.855844
min -0.870833 -1.170833 -0.929167 0.000000
25% 0.815278 -0.331944 -0.172222 1.607083
50% 0.968056 -0.262500 -0.058333 3.214167
75% 0.998611 -0.058681 0.073611 4.821250
max 1.986111 1.538889 1.348611 6.428333
//_______________Sujeito 2 Experiencia 1_______________\\
X Y Z Time (min)
count 18026.000000 18026.000000 18026.000000 18026.000000
mean 0.810453 -0.147388 0.004393 3.004167
std 0.389891 0.387617 0.368416 1.734601
min -0.563889 -1.227778 -1.493056 0.000000
25% 0.755903 -0.370833 -0.220833 1.502083
50% 0.945833 -0.265278 0.029167 3.004167
75% 1.009722 -0.105556 0.147222 4.506250
max 1.976389 1.476389 1.333333 6.008333
//_______________Sujeito 2 Experiencia 2_______________\\
X Y Z Time (min)
count 16565.000000 16565.000000 16565.000000 16565.000000
mean 0.829062 -0.164487 0.221480 2.760667
std 0.363335 0.409688 0.236887 1.594016
min -0.594444 -1.388889 -0.898611 0.000000
25% 0.738889 -0.384722 0.077778 1.380333
50% 0.952778 -0.288889 0.169444 2.760667
75% 0.993056 -0.105556 0.283333 4.141000
max 1.984722 1.173611 1.304167 5.521333
//_______________Sujeito 3 Experiencia 1_______________\\
X Y Z Time (min)
count 20994.000000 20994.000000 20994.000000 20994.000000
mean 0.878041 -0.094788 0.028095 3.498833
std 0.326701 0.396218 0.254908 2.020197
min -0.136111 -1.243056 -0.647222 0.000000
25% 0.818056 -0.306944 -0.144444 1.749417
50% 0.986111 -0.227778 -0.041667 3.498833
75% 1.013889 -0.019444 0.098264 5.248250
max 1.995833 1.209722 1.200000 6.997667
//_______________Sujeito 3 Experiencia 2_______________\\
X Y Z Time (min)
count 17493.000000 17493.000000 17493.000000 17493.000000
mean 0.851377 -0.033049 -0.005243 2.915333
std 0.341561 0.429473 0.277169 1.683313
min -1.043056 -1.019445 -0.734722 0.000000
25% 0.769444 -0.286111 -0.180556 1.457667
50% 0.959722 -0.191667 -0.076389 2.915333
75% 1.015278 0.040278 0.055556 4.373000
max 1.969445 1.288889 1.176389 5.830667
//_______________Sujeito 4 Experiencia 1_______________\\
X Y Z Time (min)
count 17668.000000 17668.000000 17668.000000 17668.000000
mean 0.867816 0.061476 0.181707 2.944500
std 0.369651 0.368682 0.164949 1.700152
min -0.211111 -1.030556 -0.773611 0.000000
25% 0.827778 -0.140278 0.088889 1.472250
50% 0.995833 -0.051389 0.156944 2.944500
75% 1.025000 0.087500 0.231944 4.416750
max 1.965278 1.140278 1.331944 5.889000
//_______________Sujeito 4 Experiencia 2_______________\\
X Y Z Time (min)
count 15888.000000 15888.000000 15888.000000 15888.000000
mean 0.867916 0.114830 0.121352 2.647833
std 0.372438 0.391490 0.148444 1.528872
min -0.237500 -1.088889 -0.530556 0.000000
25% 0.811111 -0.087500 0.050000 1.323917
50% 0.993056 -0.015278 0.109722 2.647833
75% 1.029167 0.175347 0.168056 3.971750
max 1.969445 1.323611 1.325000 5.295667
users_exp = pd.read_pickle("users.dat")
user1_1, user1_2, user2_1, user2_2, user3_1, user3_2, user4_1, user4_2 = users_exp
def plot_with_labels(upxvalues, downxvalues, up, down, user, index):
'''
Plots the X, Y, Z axis from the accelerometer
Parameters:
upxvalues (list): ordenadas para impressão de up (labels a colocar em cima)
downxvalues (list): ordenadas para impressão de down (labels a colocar em baixo)
up (list): lista com as labels das atividades
down (list): list com as labels das atividades
user (pandas.dataFrame): dataFrame com os dados do user a imprimir.
index (int): numero da experiência, de modo a imprimir o Numero do Sujeito, bem como da sua experiência
'''
figure, subplots = plt.subplots(nrows=3, ncols=1, figsize=(16,8))
figure.suptitle(f"Valores Obtidos Pelo Acelerómetro na Experiência {index%2+1} do Sujeito {index//2+1}", fontsize = 'xx-large')
#X
subplots[0].plot( user['Time (min)'], user['X'], lw = 0.3)
subplots[0].set_xlabel("Time (min)")
subplots[0].set_ylabel("ACC_X")
subplots[0].set_xlim(0, max(user['Time (min)']))
#Y
subplots[1].plot( user['Time (min)'], user['Y'], lw = 0.3)
subplots[1].set_xlabel("Time (min)")
subplots[1].set_ylabel("ACC_Y")
subplots[1].set_xlim(0, max(user['Time (min)']))
#Z
subplots[2].plot( user['Time (min)'], user['Z'], lw = 0.3)
subplots[2].set_xlabel("Time (min)")
subplots[2].set_ylabel("ACC_Z")
subplots[2].set_xlim(0, max(user['Time (min)']))
xmin,xmax = subplots[0].get_xlim()
for i in range(len(upxvalues)):
yv = 0.95 if i%2==0 else 0.90
frase = up[i]
xv = upxvalues[i]/xmax
subplots[0].annotate(xycoords = 'axes fraction', text = frase, xy = (xv,yv), textcoords = subplots[0].transAxes, xytext = (xv,yv), fontweight='bold')
subplots[1].annotate(xycoords = 'axes fraction', text = frase, xy = (xv,yv), textcoords = subplots[1].transAxes, xytext = (xv,yv), fontweight='bold')
subplots[2].annotate(xycoords = 'axes fraction', text = frase, xy = (xv,yv), textcoords = subplots[2].transAxes, xytext = (xv,yv), fontweight='bold')
yv = 0.01
for i in range(len(downxvalues)):
frase = down[i]
xv = downxvalues[i]/xmax
subplots[0].annotate(xycoords = 'axes fraction', text = frase, xy = (xv,yv), textcoords = subplots[0].transAxes, xytext = (xv,yv), fontweight='bold')
subplots[1].annotate(xycoords = 'axes fraction', text = frase, xy = (xv,yv), textcoords = subplots[1].transAxes, xytext = (xv,yv), fontweight='bold')
subplots[2].annotate(xycoords = 'axes fraction', text = frase, xy = (xv,yv), textcoords = subplots[2].transAxes, xytext =(xv,yv), fontweight='bold')
figure.tight_layout()
downintervals = [0.08, 0.44, 0.78, 1.2, 1.6, 1.9, 2.5, 3.2, 4.4, 5, 5.6]
upintervals = [0.39, 0.7, 1.1, 1.5, 1.9, 2.1, 2.8, 3.55, 4.7, 5.25, 5.8 ]
downlabels = ['STAND', 'SIT', 'STAND','LAY', 'SIT', 'LAY', 'W', 'W','W_D', 'W_D', 'W_D']
uplabels = ['STAND_SIT', 'SIT_STAND', 'STAND_LIE', 'LIE_SIT', 'SIT_LIE', 'LIE_STAND', 'W', 'W', 'W_U', 'W_U', 'W_U' ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user1_1, 0)
downintervals = [ 0.08, 0.45, 0.75, 1.3, 1.55, 1.95, 2.5, 3.4, 4.4, 5, 5.6]
upintervals = [ 0.40, 0.7, 1.15, 1.5, 1.8, 2.1, 2.9, 3.8, 4.75, 5.3, 5.9 ]
downlabels = [ 'STAND', "SIT", "STAND", "LAY", "SIT", "LAY", "W", "W", "W_D", "W_D", "W_D" ]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_SIT", "SIT_LIE", "LIE_STAND", "W", "W", "W_U", "W_U", "W_U" ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels,user1_2, 1)
downintervals = [ 0.12, 0.6, 1, 1.5, 1.85, 2.2, 2.5, 3.4, 4.25, 4.9, 5.4 ]
upintervals = [ 0.45, 0.87, 1.3, 1.6, 2.05, 2.5, 2.9, 3.95, 4.6, 5.15 ]
downlabels = [ "STAND", "SIT", "STAND", "LAY", "SIT", "LIE", "W", "W", "W_U", "W_U", "W_U"]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_SIT", "SIT_LIE", "LIE_STAND", "W", "W_D", "W_D", "W_D"]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user2_1, 2)
downintervals = [ 0.2, 0.6, 0.8, 1.2, 1.5, 1.9, 2.3, 2.95, 3.8, 4.4, 4.9 ]
upintervals = [ 0.4, 0.7, 1.1, 1.4, 1.7, 2.05, 2.6, 3.5, 4.1, 4.65 ]
downlabels = [ "STAND", "SIT", "STAND", "LAY", "SIT", "LIE", "W", "W", "W_U", "W_U", "W_U" ]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_SIT", "SIT_LIE", "LIE_STAND", "W", "W_D", "W_D", "W_D" ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user2_2, 3)
downintervals = [ 0.1, 0.55, 0.9, 1.3, 1.7, 2.2, 2.6, 3.6, 4.7, 5.3, 6, 6.6]
upintervals = [ 0.45, 0.7, 1.1, 1.6, 1.95, 2.3, 3, 4.3, 5.05, 5.7, 6.3 ]
downlabels = [ "STAND", "SIT", "STAND", "LAY", "SIT", "LAY", "W", "W", "W_U", "W_U", "W_U", "W_U"]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_SIT", "SIT_LIE", "LIE_STAND", "W", "W_D", "W_D", "W_D", "W_D" ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user3_1, 4)
downintervals = [ 0.2, 0.7, 1, 1.5, 1.9, 2.3, 2.6, 3.5, 4.3, 4.9, 5.45 ]
upintervals = [ 0.6, 0.8, 1.3, 1.7, 2.1, 2.5, 3, 4, 4.6, 5.2 ]
downlabels = [ "STAND", "SIT", "STAND", "LAY", "SIT", "LAY", "W", "W", "W_U", "W_U", "W_U", "W_U"]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_SIT", "SIT_LIE", "LIE_STAND", "W", "W_D", "W_D", "W_D", "W_D" ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user3_2, 5)
downintervals = [ 0.15, 0.6, 0.9, 1.3, 1.7, 2.1, 2.45, 3.3, 4.3, 4.9, 5.45 ]
upintervals = [ 0.4, 0.7, 1.1, 1.6, 1.9, 2.3, 2.9, 3.8, 4.6, 5.2 ]
downlabels = [ "STAND", "SIT", "STAND", "LAY", "STAND", "LAY", "W", "W", "W_U", "W_U", "W_U", "W_U"]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_STAND", "STAND_LIE", "LIE_STAND", "W", "W_D", "W_D", "W_D", "W_D" ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user4_1, 6)
downintervals = [ 0.15, 0.6, 0.9, 1.3, 1.6, 2, 2.4, 3.1, 3.9, 4.4, 4.9 ]
upintervals = [ 0.4, 0.7, 1.1, 1.6, 1.9, 2.2, 2.7, 3.6, 4.2, 4.6 ]
downlabels = ["STAND", "SIT", "STAND", "LAY", "STAND", "LAY", "W", "W", "W_U", "W_U", "W_U", "W_U"]
uplabels = [ "STAND_SIT", "SIT_STAND", "STAND_LIE", "LIE_STAND", "STAND_LIE", "LIE_STAND", "W", "W_D", "W_D", "W_D", "W_D" ]
plot_with_labels(upintervals, downintervals, uplabels, downlabels, user4_2, 7)